using FairyGUI;
using System;

namespace ABA
{
//     // 系统级的强制对话框，一般是热更及错误，玩家必须退出游戏或者重新登录
    public class SysDlg : Singleton<SysDlg>
    {
        public enum E_SYSDLG_TYPE{
            NONE,
            SYSMSG,
            RECONNECT,
            HOTUPDATE
        }

        private class SysDlgWin : FGUIWindow
        {
            SysDlg proxy;
            public SysDlgWin(FGUIWinInfo type):base(type){
            }

            protected override void OnInit()
            {
                base.OnInit();
                Center();
            }

            public void SetProxy(SysDlg p)
            {
                proxy = p;
            }

            // private void _SetFontForTF(GTextField tf, string name){
            //     var tft = tf.textFormat;
            //     tft.font = name;
            //     tf.textFormat = tft; 
            // }   

            // public void SetFont(string name)
            // {
            //     bool bShow = false;
            //     if (contentPane == null) bShow = true;

            //     if (bShow) Show();

            //     var sys = contentPane.GetChild("sys_msg").asCom;
            //     var rec = contentPane.GetChild("reconnect").asCom;
            //     var hot = contentPane.GetChild("hot_update").asCom;

            //     _SetFontForTF(sys.GetChild("txt").asTextField, name);
            //     _SetFontForTF(sys.GetChild("title").asTextField, name);
            //     _SetFontForTF(sys.GetChild("btn1").asButton.GetChild("title").asTextField, name);

            //     _SetFontForTF(rec.GetChild("txt").asTextField, name);
            //     _SetFontForTF(rec.GetChild("title").asTextField, name);
            //     _SetFontForTF(rec.GetChild("btn1").asButton.GetChild("title").asTextField, name);
            //     _SetFontForTF(rec.GetChild("btn2").asButton.GetChild("title").asTextField, name);

            //     _SetFontForTF(hot.GetChild("txt").asTextField, name);
            //     _SetFontForTF(hot.GetChild("title").asTextField, name);
            //     _SetFontForTF(hot.GetChild("btn1").asButton.GetChild("title").asTextField, name);

            //     if (bShow) HideImmediately();
            // }


            public void SetDlgType(E_SYSDLG_TYPE type, string strText, Action cb1, Action cb2)
            {
                contentPane.GetChild("sys_msg").asCom.visible = false;
                contentPane.GetChild("reconnect").asCom.visible = false;
                contentPane.GetChild("hot_update").asCom.visible = false;
                if(E_SYSDLG_TYPE.SYSMSG == type){
                    contentPane.GetChild("sys_msg").asCom.visible = true;
                    contentPane.GetChild("sys_msg").asCom.GetChild("txt").asTextField.text = strText;
                    contentPane.GetChild("sys_msg").asCom.GetChild("btn1").asButton.onClick.Set(()=>{
                        cb1();
                        proxy.Hide();
                    });

                }
                else if(E_SYSDLG_TYPE.RECONNECT == type){
                    contentPane.GetChild("reconnect").asCom.visible = true;
                    contentPane.GetChild("reconnect").asCom.GetChild("btn1").asButton.onClick.Set(()=>{
                        cb1();
                        proxy.Hide();
                    });
                    contentPane.GetChild("reconnect").asCom.GetChild("btn2").asButton.onClick.Set(()=>{
                        cb2();
                        proxy.Hide();
                    });

                }
                else if(E_SYSDLG_TYPE.HOTUPDATE == type){
                    contentPane.GetChild("hot_update").asCom.visible = true;
                    contentPane.GetChild("hot_update").asCom.GetChild("txt").asTextField.text = strText;
                    contentPane.GetChild("hot_update").asCom.GetChild("btn1").asButton.onClick.Set(()=>{
                        cb1();
                        proxy.Hide();
                    });
                }

            }
        }

        
        SysDlgWin wind;
        E_SYSDLG_TYPE _type;
        protected override void Init(){
            _type = E_SYSDLG_TYPE.NONE;

            var SYS_DIALOG = new FGUIWinInfo
            {
                cls_name = "SysDlgWin",
                pkg_name = "sys",
                comp_name = "sysdlg",
                zindex = E_FGUIWIN_ZINDEX.SYSDIALOG,
                ani = true
            };

            wind = new SysDlgWin(SYS_DIALOG);
            wind.SetProxy(this);
        }

        public void ShowSysMsg(string strMsg, Action cb1){
            if(_type != E_SYSDLG_TYPE.NONE) return;
            _type = E_SYSDLG_TYPE.SYSMSG;
            wind.Show();
            wind.SetDlgType(_type, strMsg, cb1, null);
        }

        public void ShowReconnect(Action exit, Action reconnect){
            if(_type != E_SYSDLG_TYPE.NONE) return;            
            _type = E_SYSDLG_TYPE.RECONNECT;            
            wind.Show();
            wind.SetDlgType(_type, null, exit, reconnect);
        }

        public void ShowHotUpdate(string strMsg, Action cb1){
            if(_type != E_SYSDLG_TYPE.NONE) return;            
            _type = E_SYSDLG_TYPE.HOTUPDATE;            
            wind.Show();
            wind.SetDlgType(_type, strMsg, cb1, null);
        }

        public void Hide(){
            _type = E_SYSDLG_TYPE.NONE;
            wind.HideImmediately();
        }

        // public void SetFont(string name)
        // {
        //     wind.SetFont(name);
        // }
    }

    //eg: tance.Show("你确定要关闭吗？", "提示框");
}
